package com.fjec.base.controller;

import com.fjec.base.service.IBizLogicService;
import com.fjec.base.service.ISysAnnexService;
import com.fjec.common.protocol.JsonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 业务逻辑类，后期要放在边缘服务里
 */
@RestController
public class BizLogicController {

    @Resource
    private IBizLogicService bizLogicService;

    @Resource
    private ISysAnnexService sysAnnexService;

    /**
     * 根据业务类型、业务Id获取轮播图片
     *
     * @param bizType
     * @param bizId
     * @return
     */
    @GetMapping("/n/serv/rotate/pic")
    public JsonResult getRotatePic(@RequestParam(required = false) Integer bizType,
                                   @RequestParam(required = false) Long bizId) {
        Integer type = bizType;
        Long id = bizId;
        // 如果此两项为空则设置为首页固定轮播图bizType=7，bizId=1L
        if (bizType == null || bizType == 0) {
            type = 7;
        }
        if (bizId == null || bizId == 0) {
            id = 1L;
        }
        List<String> resultList = bizLogicService.getRotatePic(type, id);
        return JsonResult.success()
                .data(resultList)
                .build();
    }

    /**
     * 首页轮播图片
     *
     * @return
     */
    @GetMapping("/n/serv/rotate/pic/v1")
    public JsonResult getRotatePicV1() {
        List<Map<String, Object>> resultList = bizLogicService.getRotatePicV1(7, 1L);
        return JsonResult.success()
                .data(resultList)
                .build();
    }

    /**
     * 根据业务Id、业务类型、业务Code获取图片
     *
     * @param bizId 可空
     * @param bizType
     * @param bizCodes
     * @return
     */
    @GetMapping("/n/serv/background/pic/v1")
    public JsonResult getBackgroundPic(@RequestParam(value = "bizId", required = false) Long bizId,
                                       @RequestParam("bizType") Integer bizType,
                                       @RequestParam("bizCodes") String bizCodes) {
        Long id = bizId;
        if (bizId == null || bizId == 0) {
            id = 1L;
        }
        String[] codes = bizCodes.split(",");
        List<Integer> codeList = new ArrayList<>();
        for (String code : codes) {
            codeList.add(Integer.valueOf(code));
        }
        return bizLogicService.getBackgroundPic(id, bizType, codeList);
    }

    @GetMapping("/y/get/secret/file")
    public void getSecretFileById(Long id, HttpServletResponse res) throws Exception {
        sysAnnexService.getSecretOSSFile(id, res);
    }
}
